<template>
    <div class="scene-wrap">
        <el-card v-show="showSearch" class="search-wrap">
            <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px"
                style="margin-bottom: -20px">
                <el-form-item :label="$t('alert.scene-list.591934-1')" prop="sceneName">
                    <el-input v-model="queryParams.sceneName" :placeholder="$t('alert.scene-list.591934-2')" clearable
                        size="small" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('search')
                    }}</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="handleResetQuery">{{ $t('reset')
                    }}</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card class="main-card" shadow="never">
            <div class="card-toolbar mb8">
                <div>
                    <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd"
                        v-hasPermi="['iot:scene:add']">{{ $t('add') }}</el-button>
                    <el-button type="primary" style="margin-left: 20px" size="mini" icon="el-icon-plus"
                        @click="goflowEditor" v-hasPermi="['iot:scene:add']">{{ $t('index.scene.20250324-1')
                        }}</el-button>
                </div>
            </div>
            <el-table class="base-table" v-loading="loading" :data="sceneList"
                @selection-change="handleSelectionChange">
                <el-table-column :label="$t('alert.scene-list.591934-1')" align="center" prop="sceneName" />
                <el-table-column :label="$t('alert.index.236501-44')" align="center" prop="enable" width="">
                    <template slot-scope="scope">
                        <el-tag type="success" size="small" v-if="scope.row.enable == 1">{{ $t('alert.index.236501-45')
                        }}</el-tag>
                        <el-tag type="danger" size="small" v-if="scope.row.enable == 2 || scope.row.enable == 0">{{
                            $t('alert.index.236501-46') }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('scene.index.670805-0')" align="center" prop="hasAlert">
                    <template slot-scope="scope">
                        <span v-if="scope.row.hasAlert == 1">{{ $t('scene.index.670805-1') }}</span>
                        <span v-if="scope.row.hasAlert == 2">{{ $t('scene.index.670805-2') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('alert.index.236501-19')" align="center" prop="cond">
                    <template slot-scope="scope">
                        <span v-if="scope.row.cond == 1">{{ $t('alert.index.236501-20') }}</span>
                        <span v-if="scope.row.cond == 2">{{ $t('alert.index.236501-21') }}</span>
                        <span v-if="scope.row.cond == 3">{{ $t('alert.index.236501-22') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('alert.index.236501-23')" align="center" prop="executeMode">
                    <template slot-scope="scope">
                        <span v-if="scope.row.executeMode == 1">{{ $t('alert.index.236501-24') }}</span>
                        <span v-if="scope.row.executeMode == 2">{{ $t('alert.index.236501-25') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('scene.index.670805-3')" align="center" prop="silentPeriod" width="">
                    <template slot-scope="scope">
                        <span>{{ scope.row.silentPeriod }} {{ $t('scene.index.670805-4') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('scene.index.670805-6')" align="center" prop="executeDelay" width="">
                    <template slot-scope="scope">
                        <span>{{ scope.row.executeDelay }} {{ $t('scene.index.670805-5') }}</span>
                    </template>
                </el-table-column>

                <el-table-column :label="$t('creatTime')" align="center" prop="createTime" width="">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('opation')" align="center" min-width="160">
                    <template slot-scope="scope">
                        <el-button size="mini" plain style="padding: 5px" type="primary" icon="el-icon-view"
                            @click="handleUpdate(scope.row)" v-hasPermi="['iot:scene:query']">{{ $t('look')
                            }}</el-button>
                        <el-button size="mini" plain style="padding: 5px" type="danger" icon="el-icon-delete"
                            @click="handleDelete(scope.row)" v-hasPermi="['iot:scene:remove']">{{ $t('del')
                            }}</el-button>
                        <!-- <el-button size="mini" plain style="padding: 5px" type="primary" icon="el-icon-caret-right" @click="handleRun(scope.row)" v-hasPermi="['iot:scene:run']">
                            {{ $t('scene.index.670805-7') }}
                        </el-button> -->
                        <!--                        <el-button size="mini" plain style="padding: 5px" type="primary" icon="el-icon-view" @click="handleLog(scope.row)" v-hasPermi="['iot:scene:run']">{{ $t('script.349087-36') }}</el-button>-->
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum"
                :total="total" @pagination="getList" />

            <!-- 添加或修改场景联动对话框 -->
            <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="title" :visible.sync="open"
                append-to-body class="scene-config-dialog" width="900px">
                <div class="el-divider el-divider--horizontal" style="margin-top: -25px"></div>
                <el-form ref="form" :model="form" :rules="rules" label-width="90px">
                    <el-row :gutter="50">
                        <el-col :span="12">
                            <el-form-item :label="$t('alert.index.236501-18')" prop="sceneName">
                                <el-input v-model="form.sceneName" :placeholder="$t('alert.scene-list.591934-2')" />
                            </el-form-item>
                            <el-form-item :label="$t('scene.index.670805-8')">
                                <el-switch v-model="form.enable" :active-value="1" :inactive-value="2" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item :label="$t('alert.index.236501-11')" prop="remark">
                                <el-input v-model="form.remark" type="textarea" rows="4"
                                    :placeholder="$t('plzInput')" />
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <div style="height: 1px; background-color: #ddd; margin: 0 0 20px 0"></div>
                    <div class="condition-wrap">
                        <el-form-item :label="$t('scene.index.670805-9')" prop="triggers">
                            <div class="item-wrap" style="background-color: #eef3f7">
                                <el-row :gutter="16">
                                    <el-col :span="24">
                                        <span>{{ $t('scene.index.670805-10') }}</span>
                                        <el-select v-model="form.cond" :placeholder="$t('scene.index.670805-11')"
                                            size="small" style="width: 160px">
                                            <el-option key="1" :label="$t('alert.index.236501-20')" :value="1" />
                                            <el-option key="2" :label="$t('alert.index.236501-21')" :value="2" />
                                            <el-option key="3" :label="$t('scene.index.670805-12')" :value="3"
                                                :disabled="formJson.triggers.length > 1" />
                                        </el-select>
                                        <span style="margin-left: 20px; font-size: 12px">{{ $t('scene.index.670805-13')
                                        }}</span>
                                    </el-col>
                                </el-row>
                            </div>
                            <div class="item-wrap" v-for="(item, index) in formJson.triggers" :key="index">
                                <el-row :gutter="16">
                                    <el-col :span="5">
                                        <el-select v-model="item.source" :placeholder="$t('pleaseSelect')" size="small"
                                            @change="handleTriggerSource($event, index)">
                                            <el-option v-for="subItem in triggerSource" :key="subItem.value"
                                                :label="subItem.label" :value="subItem.value"></el-option>
                                        </el-select>
                                    </el-col>
                                    <!-- {{ item.source }} -->
                                    <el-col :span="10" v-if="item.source == 1 || item.source == 6">
                                        <el-input readonly v-model="item.deviceCount" size="small"
                                            :placeholder="$t('product.product-things-model.142341-83')"
                                            style="margin-top: 3px">
                                            <span slot="prepend" disabled>{{ $t('scene.index.670805-14') }}</span>
                                            <el-button slot="append" @click="handleSelectDevice('trigger', item, index)"
                                                size="small">{{ $t('firmware.index.222541-31') }}</el-button>
                                        </el-input>
                                    </el-col>
                                    <el-col :span="10" v-if="item.source == 3">
                                        <el-input readonly v-model="item.productName" size="small"
                                            :placeholder="$t('device.allot-import-dialog.060657-1')"
                                            style="margin-top: 3px">
                                            <el-button slot="append"
                                                @click="handleSelectProduct('trigger', item, index)" size="small">{{
                                                    $t('sip.product-list.998536-0') }}</el-button>
                                        </el-input>
                                    </el-col>
                                    <div class="delete-wrap" v-if="index != 0">
                                        <el-button size="small" plain type="danger" style="padding: 5px"
                                            icon="el-icon-delete" @click="handleRemoveTrigger(index)">{{ $t('del')
                                            }}</el-button>
                                    </div>
                                </el-row>

                                <!--定时-->
                                <el-row v-if="item.source == 2" :gutter="16">
                                    <el-col :span="5">
                                        <el-time-picker v-model="item.timerTimeValue" :disabled="item.isAdvance == 1"
                                            format="HH:mm" size="small" style="width: 100%"
                                            :placeholder="$t('device.device-timer.433369-19')"
                                            @change="timeChange($event, index)" value-format="HH:mm"></el-time-picker>
                                    </el-col>
                                    <el-col :span="19">
                                        <el-select v-model="item.timerWeekValue" :placeholder="$t('pleaseSelect')"
                                            multiple style="width: 100%" @change="weekChange($event, index)"
                                            size="small" :disabled="item.isAdvance == 1">
                                            <el-option v-for="subItem in timerWeeks" :key="subItem.value"
                                                :label="subItem.label" :value="subItem.value"></el-option>
                                        </el-select>
                                    </el-col>
                                    <el-col :span="5" style="margin-top: 5px">
                                        <el-checkbox v-model="item.isAdvance" :false-label="0" :true-label="1" border
                                            size="small" style="width: 100%"
                                            @change="customerCronChange(($event, index))">
                                            {{ $t('scene.index.670805-15') }}
                                        </el-checkbox>
                                    </el-col>
                                    <el-col :span="19" style="margin-top: 10px">
                                        <el-input v-model="item.cronExpression"
                                            :placeholder="$t('scene.index.670805-16')" :disabled="item.isAdvance == 0"
                                            size="small">
                                            <template slot="append">
                                                <el-button :disabled="item.isAdvance == 0" type="primary"
                                                    @click="handleShowCron(item, index)">
                                                    {{ $t('scene.index.670805-17') }}
                                                    <i class="el-icon-time el-icon--right"></i>
                                                </el-button>
                                            </template>
                                        </el-input>
                                    </el-col>
                                </el-row>

                                <div v-if="item.thingsModel && item.source != 5">
                                    <!--类型和父级物模型-->
                                    <el-row :gutter="16">
                                        <el-col :span="5">
                                            <el-select v-model="item.type" :placeholder="$t('scene.index.670805-18')"
                                                size="small" @change="handleTriggerTypeChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in triggerTypes"
                                                    :key="subIndex + 'type'" :label="subItem.label"
                                                    :value="subItem.value"></el-option>
                                            </el-select>
                                        </el-col>

                                        <el-col :span="10">
                                            <el-select v-model="item.parentId" style="width: 100%"
                                                :placeholder="$t('scene.index.670805-19')" size="small"
                                                v-if="item.type === 1"
                                                @change="handleTriggerParentModelChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in item.thingsModel.properties"
                                                    :key="subIndex + 'triggerProperty'" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                            <el-select v-model="item.parentId" style="width: 100%"
                                                :placeholder="$t('scene.index.670805-19')" size="small"
                                                v-else-if="item.type == 2"
                                                @change="handleTriggerParentModelChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in item.thingsModel.functions"
                                                    :key="subIndex + 'triggerFunc'" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                            <el-select v-model="item.parentId" style="width: 100%"
                                                :placeholder="$t('scene.index.670805-19')" size="small"
                                                v-else-if="item.type == 3"
                                                @change="handleTriggerParentModelChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in item.thingsModel.events"
                                                    :key="subIndex + 'triggerEvents'" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                    </el-row>
                                    <el-row v-if="tpyeopen && item.source === 6">
                                        <el-col :span="6">
                                            <el-select v-model="item.energtype" placeholder="能源" size="small"
                                                @change="handleTriggerEnergy($event, index, item)">
                                                <el-option v-for="energy in energyList" :key="energy.id"
                                                    :label="energy.number" :value="energy.id"></el-option>
                                            </el-select>
                                        </el-col>
                                    </el-row>
                                    <!--数组索引/物模型/操作符和值-->
                                    <el-row :gutter="16">
                                        <el-col v-if="item.parentModel && item.parentModel.datatype.type === 'array'"
                                            :span="5">
                                            <el-select v-model="item.arrayIndex" :placeholder="$t('pleaseSelect')"
                                                size="small" @change="handleTriggerIndexChange($event, index)">
                                                <el-option v-for="subItem in item.parentModel.datatype.arrayModel"
                                                    :key="subItem.id" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col
                                            v-if="item.parentModel && item.parentModel.datatype.type === 'array' && item.parentModel.datatype.arrayType === 'object'"
                                            :span="5">
                                            <el-select v-model="item.id" :placeholder="$t('pleaseSelect')" size="small"
                                                @change="handleTriggerModelChange($event, index)">
                                                <el-option
                                                    v-for="(subItem, subIndex) in item.parentModel.datatype.params"
                                                    :key="subIndex" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col v-if="item.parentModel && item.parentModel.datatype.type === 'object'"
                                            :span="5">
                                            <el-select v-model="item.id" :placeholder="$t('pleaseSelect')" size="small"
                                                @change="handleTriggerModelChange($event, index)">
                                                <el-option
                                                    v-for="(subItem, subIndex) in item.parentModel.datatype.params"
                                                    :key="subIndex" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col
                                            v-if="item.model && (item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal' || item.model.datatype.type === 'string') && item.source !== 5"
                                            :span="5">
                                            <el-select v-model="item.operator"
                                                :placeholder="$t('scene.index.670805-20')" size="small">
                                                <el-option key="=" :label="$t('index.scene.20250324-17')" value="=" />
                                                <el-option key="!=" :label="$t('index.scene.20250324-18')" value="!=" />
                                                <el-option
                                                    v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                    key=">" :label="$t('index.scene.20250324-19')" value=">" />
                                                <el-option
                                                    v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                    key="<" :label="$t('index.scene.20250324-20')" value="<" />
                                                <el-option
                                                    v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                    key=">=" :label="$t('index.scene.20250324-21')" value=">=" />
                                                <el-option
                                                    v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                    key="<=" :label="$t('index.scene.20250324-22')" value="<=" />
                                                <el-option
                                                    v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                    key="between" :label="$t('index.scene.20250324-23')"
                                                    value="between" />
                                                <el-option
                                                    v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                    key="notBetween" :label="$t('index.scene.20250324-24')"
                                                    value="notBetween" />
                                                <el-option v-if="item.model.datatype.type === 'string'" key="contain"
                                                    :label="$t('index.scene.20250324-25')" value="contain" />
                                                <el-option v-if="item.model.datatype.type === 'string'" key="notContain"
                                                    :label="$t('index.scene.20250324-26')" value="notContain" />
                                            </el-select>
                                        </el-col>
                                        <el-col
                                            v-if="item.model && (item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal' || item.model.datatype.type === 'string') && item.source == 5"
                                            :span="5">
                                            <el-select v-model="item.operator"
                                                :placeholder="$t('scene.index.670805-20')" size="small">
                                                <el-option key="+" :label="$t('index.scene.20250324-13')" value="+" />
                                                <el-option key="-" :label="$t('index.scene.20250324-14')" value="-" />
                                                <el-option key="*" :label="$t('index.scene.20250324-15')" value="*" />
                                                <el-option key="/" :label="$t('index.scene.20250324-16')" value="/" />
                                            </el-select>
                                        </el-col>
                                        <el-col :span="9" v-if="item.model">
                                            <div v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                v-show="item.operator === 'between' || item.operator === 'notBetween'">
                                                <el-input v-model="item.valueA" :max="item.model.datatype.max"
                                                    :min="item.model.datatype.min"
                                                    :placeholder="$t('scene.index.670805-21')" size="small"
                                                    style="width: 95px; vertical-align: baseline" type="number"
                                                    @input="valueChange($event, item)"></el-input>
                                                <span style="padding: 0 3px; color: #999">-</span>
                                                <el-input v-model="item.valueB" :max="item.model.datatype.max"
                                                    :min="item.model.datatype.min"
                                                    :placeholder="$t('scene.index.670805-21')" size="small"
                                                    style="width: 165px; vertical-align: baseline" type="number"
                                                    @input="valueChange($event, item)">
                                                    <template slot="append">{{ item.model.datatype.unit }}</template>
                                                </el-input>
                                            </div>

                                            <div v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'"
                                                v-show="item.operator !== 'between' && item.operator !== 'notBetween'">
                                                <el-input style="vertical-align: baseline" v-model="item.value"
                                                    :placeholder="$t('scene.index.670805-21')" size="small">
                                                    <template slot="append">{{ item.model.datatype.unit }}</template>
                                                </el-input>
                                            </div>
                                            <div v-else-if="item.model.datatype.type === 'bool'">
                                                <el-switch v-model="item.value"
                                                    :inactive-text="item.model.datatype.falseText"
                                                    :active-text="item.model.datatype.trueText" active-value="1"
                                                    inactive-value="0" size="mini"
                                                    style="vertical-align: sub"></el-switch>
                                            </div>
                                            <div v-else-if="item.model.datatype.type === 'enum'">
                                                <el-select v-model="item.value" :placeholder="$t('pleaseSelect')"
                                                    style="width: 100%" size="small">
                                                    <el-option
                                                        v-for="(subItem, subIndex) in item.model.datatype.enumList"
                                                        :key="subIndex + 'things'" :label="subItem.text"
                                                        :value="subItem.value"></el-option>
                                                </el-select>
                                            </div>
                                            <div v-else-if="item.model.datatype.type === 'string'">
                                                <el-input v-model="item.value" :max="item.model.datatype.maxLength"
                                                    :placeholder="$t('scene.index.670805-22')" size="small" />
                                            </div>
                                        </el-col>
                                    </el-row>
                                </div>
                                <div v-for="(ite, inx) in item.sceneOperation" v-if="item.source == 5" :key="inx">
                                    <el-row :gutter="16">
                                        <el-col :span="11">
                                            <el-input v-model="ite.productName"
                                                :placeholder="$t('index.scene.20250324-10')" readonly size="small"
                                                style="margin-top: 3px">
                                                <span slot="prepend" disabled>{{ $t('index.scene.20250324-10') }}</span>
                                                <el-button slot="append" size="small"
                                                    @click="handleSelectDevice1('trigger', ite, inx, index)">{{
                                                        $t('index.scene.20250324-11') }}</el-button>
                                            </el-input>
                                        </el-col>
                                        <el-col v-if="ite.thingsModel" :span="4">
                                            <el-select v-model="ite.id" :placeholder="$t('index.scene.20250324-12')"
                                                size="small" style="width: 100%"
                                                @change="handleActionParentModelChange1($event, inx, ite, index, item)">
                                                <el-option v-for="(subItem, subIndex) in ite.thingsModel.properties"
                                                    :key="subIndex + 'actionProperty'" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col v-if="ite.id && item.sceneOperation.length > inx + 1" :span="4">
                                            <el-select v-model="ite.operator" :placeholder="$t('scene.index.670805-20')"
                                                size="small" @change="operatorChange(index)">
                                                <el-option key="+" :label="$t('index.scene.20250324-13')" value="+" />
                                                <el-option key="-" :label="$t('index.scene.20250324-14')" value="-" />
                                                <el-option key="*" :label="$t('index.scene.20250324-15')" value="*" />
                                                <el-option key="/" :label="$t('index.scene.20250324-16')" value="/" />
                                            </el-select>
                                        </el-col>
                                        <el-col :span="5" style="float: right">
                                            <el-button icon="el-icon-plus" plain size="small" style="padding: 8px"
                                                type="primary" @click="handleAddOne(index)">{{ $t('add') }}</el-button>
                                            <el-button v-if="item.sceneOperation.length > 1" icon="el-icon-delete" plain
                                                size="small" style="padding: 8px" type="danger"
                                                @click="handleDelOne(index, inx)">{{
                                                    $t('del') }}</el-button>
                                        </el-col>
                                    </el-row>
                                </div>

                                <div v-if="item.showOperator && item.source == 5">
                                    <el-row :gutter="16">
                                        <el-col :span="5">
                                            <el-select v-model="item.operator"
                                                :placeholder="$t('scene.index.670805-20')" size="small">
                                                <el-option key="=" :label="$t('index.scene.20250324-17')" value="=" />
                                                <el-option key="!=" :label="$t('index.scene.20250324-18')" value="!=" />
                                                <el-option key=">" :label="$t('index.scene.20250324-19')" value=">" />
                                                <el-option key="<" :label="$t('index.scene.20250324-20')" value="<" />
                                                <el-option key=">=" :label="$t('index.scene.20250324-21')" value=">=" />
                                                <el-option key="<=" :label="$t('index.scene.20250324-22')" value="<=" />
                                            </el-select>
                                        </el-col>
                                        <el-col :span="9">
                                            <div>
                                                <el-input v-model="item.value"
                                                    :placeholder="$t('index.scene.20250324-27')" size="small"
                                                    style="width: 165px; vertical-align: baseline" type="number">
                                                    <!-- <template slot="append">{{ jumpUnit(index) }}</template> -->
                                                </el-input>
                                            </div>
                                        </el-col>
                                    </el-row>
                                </div>
                                <div v-if="[1, 3, 5].includes(item.source)">
                                    <el-row :gutter="16">
                                        <el-col :span="5">
                                            <el-select v-model="item.sceneRule[0].ruleType" size="small"
                                                @change="handleSceneRule($event, index, item)">
                                                <el-option v-for="subItem in sceneRuleList" :key="subItem.value"
                                                    :label="subItem.label" :value="subItem.value"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col v-if="item.sceneRule[0].ruleType" :span="5">
                                            <el-input v-model="item.sceneRule[0].ruleValue" rows="1" type="number">
                                                <template slot="append">{{ item.sceneRule[0].unit }}</template>
                                            </el-input>
                                        </el-col>
                                        <el-col v-if="item.sceneRule[0].ruleType == 1" :span="5">
                                            <el-input v-model="item.sceneRule[0].ruleMun" rows="1" type="number">
                                                <template slot="append">{{ $t('index.scene.20250324-7') }}</template>
                                            </el-input>
                                        </el-col>
                                    </el-row>
                                </div>
                            </div>
                            <div v-if="!(form.cond == 3 && formJson.triggers.length > 0)">
                                +
                                <a style="color: #409eff" @click="handleAddTrigger()">{{ $t('scene.index.670805-23')
                                }}</a>
                            </div>
                        </el-form-item>
                    </div>

                    <el-divider></el-divider>
                    <div class="action-wrap">
                        <el-form-item :label="$t('scene.index.670805-24')">
                            <div class="item-wrap" style="background-color: #eef3f7">
                                <el-row :gutter="16">
                                    <el-col :span="8">
                                        <span>
                                            <el-tooltip class="item" effect="dark"
                                                :content="$t('scene.index.670805-25')" placement="top">
                                                <i class="el-icon-question"></i>
                                            </el-tooltip>
                                            {{ $t('scene.index.670805-26') }}
                                        </span>
                                        <el-input v-model="form.silentPeriod" size="small"
                                            :placeholder="$t('scene.index.670805-4')" type="number"
                                            style="width: 140px">
                                            <template slot="append">{{ $t('scene.index.670805-4') }}</template>
                                        </el-input>
                                    </el-col>
                                    <el-col :span="8">
                                        <span>{{ $t('scene.index.670805-27') }}</span>
                                        <el-select v-model="form.executeMode" :placeholder="$t('scene.index.670805-28')"
                                            size="small" style="width: 160px">
                                            <el-option key="1" :label="$t('scene.index.670805-29')" :value="1" />
                                            <el-option key="2" :label="$t('scene.index.670805-30')" :value="2" />
                                        </el-select>
                                    </el-col>
                                    <el-col :span="8">
                                        <span>
                                            <el-tooltip class="item" effect="dark"
                                                c:content="$t('scene.index.670805-31')" placement="top">
                                                <i class="el-icon-question"></i>
                                            </el-tooltip>
                                            {{ $t('scene.index.670805-32') }}
                                        </span>
                                        <el-input v-model="form.executeDelay" prop="executeDelay" size="small"
                                            :placeholder="$t('scene.index.670805-5')" :max="90" :min="0"
                                            oninput="if(value>90)value=90;if(value<0)value=0" type="number"
                                            style="width: 140px">
                                            <template slot="append">{{ $t('scene.index.670805-5') }}</template>
                                        </el-input>
                                    </el-col>
                                </el-row>
                            </div>

                            <div class="item-wrap" v-for="(item, index) in formJson.actions" :key="index">
                                <el-row :gutter="16">
                                    <el-col :span="4">
                                        <el-select v-model="item.source" :placeholder="$t('pleaseSelect')" size="small"
                                            @change="handleActionSourceChange($event, index)">
                                            <el-option v-for="subItem in actionSource" :key="subItem.value"
                                                :label="subItem.label" :value="subItem.value"></el-option>
                                        </el-select>
                                    </el-col>
                                    <el-col :span="10" v-if="item.source == 1">
                                        <el-input readonly v-model="item.deviceCount" size="small"
                                            :placeholder="$t('scene.index.670805-33')" style="margin-top: 3px">
                                            <span slot="prepend" disabled>{{ $t('scene.index.670805-14') }}</span>
                                            <el-button slot="append" @click="handleSelectDevice('action', item, index)"
                                                size="small">{{ $t('scene.index.670805-34') }}</el-button>
                                        </el-input>
                                    </el-col>
                                    <el-col :span="10" v-if="item.source == 3">
                                        <el-input readonly v-model="item.productName" size="small"
                                            :placeholder="$t('scene.index.670805-35')" style="margin-top: 3px">
                                            <el-button slot="append" @click="handleSelectProduct('action', item, index)"
                                                size="small">{{ $t('scene.index.670805-36') }}</el-button>
                                        </el-input>
                                    </el-col>
                                    <el-col :span="10" v-if="item.source == 5">
                                        <el-input readonly v-model="form.recoverSceneName" size="small"
                                            :placeholder="$t('scene.index.670805-75')" style="margin-top: 3px">
                                            <el-button slot="append"
                                                @click="handleSelectRecoverScenes('action', item, index)"
                                                size="small">{{ $t('scene.index.670805-76') }}</el-button>
                                        </el-input>
                                    </el-col>
                                    <el-col :span="10" v-if="item.source === 6">
                                        <el-form-item>
                                            <el-input v-model="item.userName" placeholder="请输入执行人"
                                                @click.native="selectUser(index)" />
                                        </el-form-item>
                                    </el-col>
                                    <div class="delete-wrap">
                                        <el-button size="small" v-if="index != 0" plain type="danger"
                                            style="padding: 5px" icon="el-icon-delete"
                                            @click="handleRemoveAction(index)">{{ $t('del') }}</el-button>
                                    </div>
                                </el-row>

                                <div v-if="item.thingsModel">
                                    <!--类型和父级物模型-->
                                    <el-row :gutter="16">
                                        <el-col :span="5">
                                            <el-select v-model="item.type" :placeholder="$t('scene.index.670805-1')"
                                                size="small" @change="handleActionTypeChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in actionTypes"
                                                    :key="subIndex + 'type'" :label="subItem.label"
                                                    :value="subItem.value"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col :span="10">
                                            <el-select style="width: 100%" v-model="item.parentId"
                                                placeholder="$t('scene.index.670805-19')" v-if="item.type == 1"
                                                size="small" @change="handleActionParentModelChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in item.thingsModel.properties"
                                                    :key="subIndex + 'actionProperty'" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                            <el-select v-model="item.parentId" size="small"
                                                :placeholder="$t('scene.index.670805-19')" v-else-if="item.type == 2"
                                                style="width: 100%"
                                                @change="handleActionParentModelChange($event, index)">
                                                <el-option v-for="(subItem, subIndex) in item.thingsModel.functions"
                                                    :key="subIndex + 'actionFunc'" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                    </el-row>

                                    <!--数组索引/物模型/值-->
                                    <el-row :gutter="16">
                                        <el-col v-if="item.parentModel && item.parentModel.datatype.type === 'array'"
                                            :span="5">
                                            <el-select v-model="item.arrayIndex" :placeholder="$t('pleaseSelect')"
                                                size="small" @change="handleActionIndexChange($event, index)">
                                                <el-option v-for="subItem in item.parentModel.datatype.arrayModel"
                                                    :key="subItem.id" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col
                                            v-if="item.parentModel && item.parentModel.datatype.type === 'array' && item.parentModel.datatype.arrayType === 'object'"
                                            :span="5">
                                            <el-select v-model="item.id" :placeholder="$t('pleaseSelect')" size="small"
                                                @change="handleActionModelChange($event, index)">
                                                <el-option
                                                    v-for="(subItem, subIndex) in item.parentModel.datatype.params"
                                                    :key="subIndex" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col v-if="item.parentModel && item.parentModel.datatype.type === 'object'"
                                            :span="5">
                                            <el-select v-model="item.id" :placeholder="$t('pleaseSelect')" size="small"
                                                @change="handleActionModelChange($event, index)">
                                                <el-option
                                                    v-for="(subItem, subIndex) in item.parentModel.datatype.params"
                                                    :key="subIndex" :label="subItem.name"
                                                    :value="subItem.id"></el-option>
                                            </el-select>
                                        </el-col>
                                        <el-col :span="10" v-if="item.model">
                                            <div
                                                v-if="item.model.datatype.type === 'integer' || item.model.datatype.type === 'decimal'">
                                                <el-input style="vertical-align: baseline" v-model="item.value"
                                                    :placeholder="$t('scene.index.670805-21')"
                                                    :max="item.model.datatype.max" :min="item.model.datatype.min"
                                                    type="number" size="small">
                                                    <template slot="append">{{ item.model.datatype.unit }}</template>
                                                </el-input>
                                            </div>
                                            <div v-else-if="item.model.datatype.type === 'bool'">
                                                <el-switch v-model="item.value"
                                                    :inactive-text="item.model.datatype.falseText"
                                                    :active-text="item.model.datatype.trueText" active-value="1"
                                                    inactive-value="0" style="vertical-align: baseline"></el-switch>
                                            </div>
                                            <div v-else-if="item.model.datatype.type === 'enum'">
                                                <el-select v-model="item.value" :placeholder="$t('pleaseSelect')"
                                                    style="width: 100%" size="small">
                                                    <el-option
                                                        v-for="(subItem, subIndex) in item.model.datatype.enumList"
                                                        :key="subIndex + 'things'" :label="subItem.text"
                                                        :value="subItem.value"></el-option>
                                                </el-select>
                                            </div>
                                            <div v-else-if="item.model.datatype.type === 'string'">
                                                <el-input v-model="item.value"
                                                    :placeholder="$t('scene.index.670805-22')" size="small"
                                                    :max="item.model.datatype.maxLength" />
                                            </div>
                                        </el-col>
                                    </el-row>
                                </div>
                            </div>
                            <div>
                                +
                                <a style="color: #409eff" @click="handleAddAction()">{{ $t('scene.index.670805-37')
                                }}</a>
                            </div>
                        </el-form-item>
                    </div>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="handleSubmitForm" :disabled="updateBtnDisabled"
                        :loading="confirmLoading" v-hasPermi="['iot:scene:edit']" v-show="form.sceneId">{{ $t('update')
                        }}</el-button>
                    <el-button type="primary" @click="handleSubmitForm" :disabled="updateBtnDisabled"
                        :loading="confirmLoading" v-hasPermi="['iot:scene:add']" v-show="!form.sceneId">{{ $t('add')
                        }}</el-button>
                    <el-button @click="cancel">{{ $t('cancel') }}</el-button>
                </div>
            </el-dialog>

            <el-dialog :title="title" :visible.sync="openLog" width="800px" append-to-body :close-on-click-modal="false"
                :close-on-press-escape="false">
                <div ref="logContainer" v-loading="logLoading" :element-loading-text="$t('script.349087-39')"
                    element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="border: 1px solid #ccc; border-radius: 5px; height: 500px; background-color: #181818; color: #fff; padding: 0 5px; line-height: 20px; overflow: auto">
                    <pre>
            {{ logs }}
        </pre>
                </div>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancelLog">{{ $t('script.349087-38') }}</el-button>
                </div>
            </el-dialog>
        </el-card>

        <!-- 设备列表 -->
        <deviceList ref="deviceList" @deviceEvent="getSelectProductDevice($event, 1)"></deviceList>

        <!-- 设备列表运算触发专用 -->
        <deviceList ref="deviceList1" :chooesOne="true" @deviceEvent="getSelectProductDevice1($event, 1)"></deviceList>
        <!-- 产品列表 -->
        <productList ref="productList" @productEvent="getSelectProductDevice($event, 2)"></productList>
        <!-- 选择场景对话框 -->
        <scene-list ref="sceneList" @sceneEvent="getSceneData($event)" />

        <!-- CRON表达式生成器 -->
        <el-dialog :title="$t('scene.index.670805-38')" :visible.sync="openCron" append-to-body destroy-on-close
            class="scrollbar">
            <crontab :expression="expression" style="padding-bottom: 10px" @fill="crontabFill" @hide="openCron = false">
            </crontab>
        </el-dialog>

        <!-- 选择用户 -->
        <user-list ref="userList" @userEvent="getUserData($event)" />
    </div>
</template>

<script>
import { findPurchasing } from '@/views/energy/api/energy/deviceAreaBinding.js';
import { addScene, delScene, getEnergyType, getScene, listScene, runScene, updateScene } from '@/api/iot/scene';
import { cacheJsonThingsModel } from '@/api/iot/model';
import Crontab from '@/components/Crontab';
import deviceList from './device-list';
import productList from './product-list';
import sceneList from './scene-list';
import AceEditor from '@/views/components/editor/editor.vue';
import userList from '@/views/iot/sip/user-list.vue';
import { Message } from 'element-ui';
export default {
    name: 'scene',
    components: {
        AceEditor,
        deviceList,
        productList,
        sceneList,
        Crontab,
        userList,
    },
    computed: {
        // 计算两个周期值
        jumpIsShowOperator() {
            let isShow = false;
            if (this.formJson.triggers[index].sceneOperation) {
                this.formJson.triggers[index].sceneOperation.map((item) => {
                    if (item.id) {
                        isShow = true;
                    }
                });
            }
            return isShow;
        },
    },
    data() {
        return {
            // 确认按钮加载
            confirmLoading: false,
            // 更新按钮
            updateBtnDisabled: false,
            // 当前选择设备时的类型（trigger/action）
            currentType: null,
            // 当前选择设备时的类型索引
            currentIndex: null,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 场景联动表格数据
            sceneList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            logs: '',
            logChainID: '',
            // 是否显示Cron表达式弹出层
            openCron: false,
            openLog: false,
            logLoading: false,
            // 传入的表达式
            expression: '',
            // 触发器的索引，用于接收传入的表达式
            triggerIndex: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                sceneName: null,
                userId: null,
                userName: null,
            },
            timerWeeks: [
                {
                    value: 1,
                    label: this.$t('scene.index.670805-39'),
                },
                {
                    value: 2,
                    label: this.$t('scene.index.670805-40'),
                },
                {
                    value: 3,
                    label: this.$t('scene.index.670805-41'),
                },
                {
                    value: 4,
                    label: this.$t('scene.index.670805-42'),
                },
                {
                    value: 5,
                    label: this.$t('scene.index.670805-43'),
                },
                {
                    value: 6,
                    label: this.$t('scene.index.670805-44'),
                },
                {
                    value: 7,
                    label: this.$t('scene.index.670805-45'),
                },
            ],
            // 触发器源 1=设备，2=定时
            triggerSource: [
                {
                    value: 1,
                    label: this.$t('scene.index.670805-46'),
                },
                {
                    value: 2,
                    label: this.$t('scene.index.670805-47'),
                },
                {
                    value: 3,
                    label: this.$t('scene.index.670805-48'),
                },
                {
                    value: 4,
                    label: this.$t('scene.index.670805-49'),
                },
                {
                    value: 5,
                    label: this.$t('index.scene.20250324-8'),
                },
                {
                    value: 6,
                    label: this.$t('index.scene.20250324-9'),
                },
            ],
            sceneRuleList: [
                {
                    value: 0,
                    unit: '',
                    label: this.$t('index.scene.20250324-2'),
                },
                {
                    value: 1,
                    unit: this.$t('index.scene.20250324-5'),
                    label: this.$t('index.scene.20250324-3'),
                },
                {
                    value: 2,
                    unit: this.$t('index.scene.20250324-6'),
                    label: this.$t('index.scene.20250324-4'),
                },
            ],
            // 触发器类别
            triggerTypes: [
                {
                    value: 1,
                    label: this.$t('scene.index.670805-50'),
                },
                {
                    value: 2,
                    label: this.$t('scene.index.670805-51'),
                },
                {
                    value: 3,
                    label: this.$t('scene.index.670805-52'),
                },
                {
                    value: 5,
                    label: this.$t('scene.index.670805-53'),
                },
                {
                    value: 6,
                    label: this.$t('scene.index.670805-54'),
                },
            ],
            // 动作源 1=设备，3=产品执行，4=告警执行
            actionSource: [
                {
                    value: 1,
                    label: this.$t('scene.index.670805-55'),
                },
                {
                    value: 3,
                    label: this.$t('scene.index.670805-56'),
                },
                {
                    value: 4,
                    label: this.$t('scene.index.670805-57'),
                },
                {
                    value: 5,
                    label: this.$t('scene.index.670805-58'),
                },
                {
                    value: 6,
                    label: this.$t('index.scene.20250324-28'),
                },
            ],
            // 动作类别
            actionTypes: [
                {
                    value: 1,
                    label: this.$t('scene.index.670805-50'),
                },
                {
                    value: 2,
                    label: this.$t('scene.index.670805-51'),
                },
            ],
            // 表单参数
            formJson: {
                triggers: [
                    {
                        // 时间
                        timerTimeValue: '',
                        // 星期
                        timerWeekValue: [1, 2, 3, 4, 5, 6, 7],
                        sceneRule: [
                            {
                                ruleType: 0,
                                unit: '',
                                ruleValue: '',
                                ruleMun: null,
                            },
                        ],
                    },
                ],
                actions: [],
            },
            actionIndex: null,
            form: {},
            // 表单校验
            rules: {
                sceneName: [
                    {
                        required: true,
                        message: this.$t('scene.index.670805-59'),
                        trigger: 'blur',
                    },
                ],
                executeDelay: [
                    {
                        required: true,
                        max: 90,
                        min: 0,
                        message: this.$t('scene.index.670805-60'),
                        trigger: 'blur',
                    },
                ],
                checkDelay: [
                    {
                        required: true,
                        max: 600,
                        min: 0,
                        message: this.$t('scene.index.670805-61'),
                        trigger: 'blur',
                    },
                ],
            },
            // 运算出发专用
            currentType1: null,
            // 当前选择设备时的类型索引
            currentIndex1: null,
            // 父级的索引
            currentIndexItem: null,
            // 能源列表
            energyList: [],
            tpyeopen: false,
        };
    },
    created() {
        this.connectMqtt();
        this.getList();
        this.getEnergyList();
    },
    methods: {
        goflowEditor() {
            this.$router.push({
                path: '/iotdev/ruleengine/flowEditor',
            });
        },
        // 获取能源列表
        getEnergyList() {
            findPurchasing().then((response) => {
                this.energyList = response.data;
            });
        },
        getUserData(user) {
            // console.log('user', userName);

            this.formJson.actions[this.actionIndex].userId = user.userId;
            this.formJson.actions[this.actionIndex].userName = user.userName;
            console.log(this.formJson.actions, this.formJson.actions);

            /* if (user) {
                this.$set(this.formData.repair, 'repairPerson', user.userId)
                this.$set(this.formData.repair, 'repairPersonName', user.nickName)
            }*/
        },
        selectUser(index) {
            console.log('this.formJson', this.formJson);
            // return;
            this.actionIndex = index;
            this.$refs.userList.open = true;
            if (this.formJson.actions[index].userId) {
                this.$refs.userList.getList(this.formJson.actions[index].userId);
            } else {
                this.$refs.userList.getList();
            }
        },
        /* 连接Mqtt消息服务器 */
        async connectMqtt() {
            if (this.$mqttTool.client == null) {
                await this.$mqttTool.connect();
            }
            // 删除所有message事件监听器
            this.$mqttTool.client.removeAllListeners('message');
            // 添加message事件监听器
            this.mqttCallback();
        },
        /** 查询场景联动列表 */
        getList() {
            this.loading = true;
            listScene(this.queryParams).then((response) => {
                this.sceneList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                sceneId: null,
                recoverId: null,
                recoverSceneName: null,
                sceneName: null,
                userId: null,
                userName: null,
                remark: null,
                enable: 1,
                cond: 1,
                silentPeriod: 0,
                executeMode: 1,
                executeDelay: 0,
                checkDelay: 0,
                hasAlert: 2, // 1=包含告警，2=不包含告警
                applicationName: 'yanfan', // 固定值
            };
            this.formJson = {
                triggers: [
                    {
                        productId: 0,
                        productName: '',
                        deviceCount: 0,
                        deviceNums: [],
                        source: 1, //1=设备，2=定时，3=产品
                        type: 1, // 类型1=属性/2=功能，3=事件
                        parentId: '', // 物模父级id
                        parentName: '',
                        parentModel: null,
                        model: null,
                        operator: '',
                        id: '',
                        name: '',
                        value: '', // between操作符时，值=值A-值B
                        valueA: '',
                        valueB: '',
                        arrayIndex: '', // 索引，数组才有
                        arrayIndexName: '',
                        isAdvance: 0, // 自定义CRON
                        cronExpression: '', // cron表达式
                        timerTimeValue: '', // 时间
                        timerWeekValue: [1, 2, 3, 4, 5, 6, 7], // 星期
                        scriptPurpose: 3, // scriptPurpose:脚本用途(1=数据流，2=触发器，3=执行动作)
                        sceneRule: [
                            {
                                ruleType: 0,
                                unit: '',
                                ruleValue: '',
                            },
                        ],
                    },
                ],
                actions: [
                    {
                        productId: 0,
                        productName: '',
                        deviceCount: 0,
                        deviceNums: [],
                        source: 1, //1=设备，3=产品，4=告警，5=告警恢复
                        type: 2, // 类型1=属性/2=功能，3=事件
                        parentId: '', // 物模父级id
                        parentName: '',
                        parentModel: null, // 父级物模型
                        model: null, // 物模型
                        id: '',
                        name: '',
                        value: '',
                        arrayIndex: '', // 索引，数组才有
                        arrayIndexName: '',
                        scriptPurpose: 3, // scriptPurpose:脚本用途(1=数据流，2=触发器，3=执行动作)
                        userId: '',
                        userName: '',
                    },
                ],
            };
            this.confirmLoading = false;
            this.updateBtnDisabled = false;
            this.resetForm('form');
        },
        // 触发器中，选择between操作符，值是A值和B值用中划线分割
        valueChange(value, item) {
            item.value = item.valueA + '-' + item.valueB;
        },
        // 搜索按钮操作value,item
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        // 重置按钮操作
        handleResetQuery() {
            this.resetForm('queryForm');
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.sceneId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        // 新增按钮操
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = this.$t('scene.index.670805-62');
        },
        // 修改按钮操作
        handleUpdate(row) {
            this.reset();
            const sceneId = row.sceneId || this.ids;
            getScene(sceneId).then((response) => {
                const datas = JSON.parse(JSON.stringify(response));
                this.form = response.data;
                // triggers赋值
                this.formJson.triggers = this.form.triggers;
                for (let i = 0; i < this.formJson.triggers.length; i++) {
                    // 定时
                    if (this.formJson.triggers[i].source == 2) {
                        if (this.formJson.triggers[i].isAdvance == 0) {
                            let arrayValue = this.formJson.triggers[i].cronExpression.substring(12).split(',').map(Number);
                            this.formJson.triggers[i].timerWeekValue = arrayValue;
                            let timerTimeValue = this.formJson.triggers[i].cronExpression.substring(5, 7) + ':' + this.formJson.triggers[i].cronExpression.substring(2, 4);
                            // 解决时间选择器不能编辑问题
                            this.$set(this.formJson.triggers[i], 'timerTimeValue', timerTimeValue);
                        }
                    } else if (this.formJson.triggers[i].source == 4) {
                    } else if (this.formJson.triggers[i].source == 5) {
                        this.formJson.triggers[i].sceneOperation.map((item, index) => {
                            item.deviceCount = 1;
                            item.deviceNums = [item.serialNumber];
                            item.scriptPurpose = 2;
                            this.formatSceneScript(this.formJson.triggers[i].sceneOperation[index], i);
                        });
                        console.log(this.formJson.triggers[i].sceneOperation, '9999999');
                        // 获取设备和物模型详情

                        this.$set(this.formJson.triggers[i], 'showOperator', true);
                    } else if (this.formJson.triggers[i].source === 6) {
                        this.formJson.triggers[i].energtype = this.formJson.triggers[i].sceneEneregy.energtype;
                        this.formatSceneScript(this.formJson.triggers[i], i);
                    } else {
                        // 获取设备和物模型详情
                        this.formatSceneScript(this.formJson.triggers[i], i);
                    }
                }
                // actions赋值
                this.formJson.actions = this.form.actions;
                for (let i = 0; i < this.formJson.actions.length; i++) {
                    // 获取设备和物模型详情
                    this.formatSceneScript(this.formJson.actions[i], i);
                }
                // 按钮可用
                setTimeout(() => {
                    this.updateBtnDisabled = false;
                }, 2000);
                this.open = true;
                this.title = this.$t('scene.index.670805-63');
            });
        },
        /**
         * 格式化显示场景脚本（触发器和执行动作）
         * sceneScript:触发器或动作
         * sceneScript.scriptPurpose：脚本用途(1=数据流，2=触发器，3=执行动作)
         */
        formatSceneScript(sceneScript, index) {
            if (sceneScript.scriptPurpose == 2) {
                // 获取物模型
                cacheJsonThingsModel(sceneScript.productId).then((response) => {
                    let data = JSON.parse(response.data);
                    sceneScript.thingsModel = this.formatArrayIndex(data);
                    // value赋值，value=valueA-valueB
                    if (sceneScript.value && sceneScript.value.indexOf('-') != -1) {
                        let values = sceneScript.value.split('-');
                        sceneScript.valueA = values[0];
                        sceneScript.valueB = values[1];
                    }
                    let sceneScripts = [];
                    if (sceneScript.type == 1) {
                        // 属性
                        sceneScripts = sceneScript.thingsModel.properties;
                    } else if (sceneScript.type == 2) {
                        // 功能
                        sceneScripts = sceneScript.thingsModel.functions;
                    } else if (sceneScript.type == 3) {
                        // 事件
                        sceneScripts = sceneScript.thingsModel.events;
                    }
                    // 父级物模型和物模型赋值
                    this.setParentAndModelData(sceneScript, sceneScripts);
                    // 解决数组在界面中不更新问题
                    this.$set(this.formJson.triggers, index, this.formJson.triggers[index]);
                });
            } else if (sceneScript.scriptPurpose == 3) {
                // 执行告警，没有物模型
                if (sceneScript.source == 4 || sceneScript.source == 6) {
                    return;
                }
                // 获取物模型
                cacheJsonThingsModel(sceneScript.productId).then((response) => {
                    let data = JSON.parse(response.data);
                    sceneScript.thingsModel = this.formatArrayIndex(data);
                    // 过滤监测数据和只读数据
                    if (sceneScript.thingsModel.properties) {
                        sceneScript.thingsModel.properties = sceneScript.thingsModel.properties.filter((item) => item.isMonitor == 0 && item.isReadonly == 0);
                        for (let i = 0; i < sceneScript.thingsModel.properties.length; i++) {
                            if (sceneScript.thingsModel.properties[i].datatype.params) {
                                sceneScript.thingsModel.properties[i].datatype.params = sceneScript.thingsModel.properties[i].datatype.params.filter((item) => item.isReadonly == 0);
                            }
                        }
                    }
                    if (sceneScript.thingsModel.functions) {
                        sceneScript.thingsModel.functions = sceneScript.thingsModel.functions.filter((item) => item.isReadonly == 0);
                        for (let i = 0; i < sceneScript.thingsModel.functions.length; i++) {
                            if (sceneScript.thingsModel.functions[i].datatype.params) {
                                sceneScript.thingsModel.functions[i].datatype.params = sceneScript.thingsModel.functions[i].datatype.params.filter((item) => item.isReadonly == 0);
                            }
                        }
                    }

                    let sceneScripts = [];
                    if (sceneScript.type == 1) {
                        // 属性
                        sceneScripts = sceneScript.thingsModel.properties;
                    } else if (sceneScript.type == 2) {
                        // 功能
                        sceneScripts = sceneScript.thingsModel.functions;
                    }
                    // 父级物模型和物模型赋值
                    this.setParentAndModelData(sceneScript, sceneScripts);
                    // 解决数组在界面中不更新问题
                    this.$set(this.formJson.actions, index, this.formJson.actions[index]);
                });
            }
        },
        // 设置父级物模型和物模型值
        setParentAndModelData(sceneScript, sceneScripts) {
            for (let i = 0; i < sceneScripts.length; i++) {
                if (sceneScript.parentId == sceneScripts[i].id) {
                    sceneScript.parentModel = sceneScripts[i];
                    if (sceneScript.parentModel.datatype.type === 'object') {
                        // 对象类型，物模型赋值
                        for (let j = 0; j < sceneScript.parentModel.datatype.params.length; j++) {
                            if (sceneScript.id == sceneScript.parentModel.datatype.params[j].id) {
                                sceneScript.model = sceneScript.parentModel.datatype.params[j];
                            }
                        }
                    } else if (sceneScript.parentModel.datatype.arrayType === 'object' && sceneScript.parentModel.datatype.type === 'array') {
                        // 对象数组类型，物模型集合移除索引前缀
                        if (sceneScript.id.indexOf('array_') != -1) {
                            sceneScript.id = sceneScript.id.substring(9);
                        }
                        // 物模型赋值
                        for (let j = 0; j < sceneScript.parentModel.datatype.params.length; j++) {
                            if (sceneScript.id == sceneScript.parentModel.datatype.params[j].id) {
                                sceneScript.model = sceneScript.parentModel.datatype.params[j];
                            }
                        }
                    } else if (sceneScript.parentModel.datatype.arrayType !== 'object' && sceneScript.parentModel.datatype.type === 'array') {
                        // 普通数组类型，物模型集合移除索引前缀
                        if (sceneScript.id.indexOf('array_') != -1) {
                            sceneScript.id = sceneScript.id.substring(9);
                        }
                        sceneScript.model = {
                            datatype: {
                                type: sceneScript.parentModel.datatype.arrayType,
                                maxLength: -1,
                                min: -1,
                                max: -1,
                                unit: this.$t('scene.index.670805-64'),
                            },
                        };
                    } else {
                        // 普通类型
                        sceneScript.model = sceneScript.parentModel;
                    }
                    break;
                }
            }
        },
        // 删除按钮操作
        handleDelete(row) {
            const sceneIds = row.sceneId || this.ids;
            this.$modal
                .confirm(this.$t('scene.index.670805-65', [sceneIds]))
                .then(function () {
                    return delScene(sceneIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess(this.$t('scene.index.670805-66'));
                })
                .catch(() => { });
        },
        // 执行一次
        handleRun(data) {
            const params = { sceneId: data.sceneId };
            runScene(params).then((res) => {
                this.$modal.msgSuccess(res.msg);
            });
        },

        /* Mqtt回调处理  */
        mqttCallback() {
            this.$mqttTool.client.on('message', (topic, message, buffer) => {
                if (message instanceof Uint8Array) {
                    // 创建TextDecoder对象来转换Uint8Array到字符串
                    const decoder = new TextDecoder('utf-8');
                    const str = decoder.decode(message);
                    message = str; //转换后的字符串
                }
                console.log('🚀 ~ this.$mqttTool.client.on ~ message:', message);
                console.log('🚀 ~ this.$mqttTool.client.on ~ topic:', topic);
                if (topic.indexOf('/' + this.logChainID + '/ruleengine/test') != -1) {
                    message = JSON.parse(message.toString());
                    this.logs += '[' + message.time + ']' + '-' + '[' + message.requestId + ']:' + message.log + '\n';
                }
            });
        },
        handleLog(data) {
            this.logLoading = true;
            openPublishLog(data.chainName).then((response) => {
                this.openLog = true;
                this.title = this.$t('scene.index.670805-67');
                this.logLoading = false;
                this.logChainID = data.chainName;
                let topic = '/' + c + '/ruleengine/test';
                this.$mqttTool.subscribe(topic);
            });
        },
        // 取消日志按钮
        cancelLog() {
            this.logs = '';
            this.openLog = false;
            closePublishLog(this.logChainID).then((response) => {
                let topic = '/' + this.logChainID + '/ruleengine/test';
                this.$mqttTool.unsubscribe(topic);
                this.logChainID = '';
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'iot/scene/export',
                {
                    ...this.queryParams,
                },
                `scene_${new Date().getTime()}.xlsx`
            );
        },
        // 选择能源
        handleTriggerEnergy(v, index) {
            // const energy = this.energyList.find(item => item.energyId == v)
            // this.formJson.triggers[index].energtype = v
            // console.log('v', v, 'index', index, energy)
            this.formJson.triggers[index].energtype = v;
        },
        // 触发器源改变事件
        handleTriggerSource(source, index) {
            this.formJson.triggers[index].deviceCount = 0;
            this.formJson.triggers[index].productId = '';
            this.formJson.triggers[index].productName = '';
            this.formJson.triggers[index].thingsModel = null;
            this.formJson.triggers[index].id = '';
            this.formJson.triggers[index].name = '';
            this.formJson.triggers[index].value = '';
            this.formJson.triggers[index].valueA = '';
            this.formJson.triggers[index].valueB = '';
            this.formJson.triggers[index].parentId = '';
            this.formJson.triggers[index].parentName = '';
            this.formJson.triggers[index].model = null;
            this.formJson.triggers[index].parentModel = null;
            this.formJson.triggers[index].operator = '';
            this.formJson.triggers[index].deviceNums = [];
            this.formJson.triggers[index].sceneOperation = [
                {
                    deviceCount: 0,
                },
            ];
            this.formJson.triggers[index].sceneRule = [
                {
                    ruleType: 0,
                    unit: '',
                    ruleValue: '',
                },
            ];
            // 定时
            this.formJson.triggers[index].timerTimeValue = ''; // 时间
            this.formJson.triggers[index].timerWeekValue = [1, 2, 3, 4, 5, 6, 7]; // 星期
        },
        handleActionSourceChange(source, index) {
            console.log('this.formJson.actions[index]', this.formJson.actions[index]);
            this.formJson.actions[index].deviceCount = 0;
            this.formJson.actions[index].productId = '';
            this.formJson.actions[index].productName = '';
            this.formJson.actions[index].thingsModel = null;
            this.formJson.actions[index].id = '';
            this.formJson.actions[index].name = '';
            this.formJson.actions[index].value = '';
            this.formJson.actions[index].valueA = '';
            this.formJson.actions[index].valueB = '';
            this.formJson.actions[index].parentId = '';
            this.formJson.actions[index].parentName = '';
            this.formJson.actions[index].model = null;
            this.formJson.actions[index].parentModel = null;
            this.formJson.actions[index].operator = '';
            this.formJson.actions[index].deviceNums = [];
            // 定时
            this.formJson.actions[index].timerTimeValue = ''; // 时间
            this.formJson.actions[index].timerWeekValue = [1, 2, 3, 4, 5, 6, 7]; // 星期
        },
        /* 选择设备
         *  type : 'trigger','action'
         */
        handleSelectDevice(type, item, index) {
            this.currentType = type;
            this.currentIndex = index;
            // 刷新子组件
            this.$refs.deviceList.queryParams.pageNum = 1;
            this.$refs.deviceList.openDeviceList = true;
            this.$refs.deviceList.selectDeviceNums = item.deviceNums;
            this.$refs.deviceList.productId = item.productId;
            this.$refs.deviceList.productName = item.productName;
            this.$refs.deviceList.queryParams.productId = item.productId;
            this.$refs.deviceList.getList();
        },
        /**选择产品 */
        handleSelectProduct(type, item, index) {
            this.currentType = type;
            this.currentIndex = index;
            // 刷新子组建
            this.$refs.productList.queryParams.pageNum = 1;
            this.$refs.productList.open = true;
            this.$refs.productList.selectProductId = item.productId;
            this.$refs.productList.getList();
        },
        /**添加场景*/
        handleSelectRecoverScenes() {
            this.$refs.sceneList.queryParams.pageNum = 1;
            this.$refs.sceneList.openScene = true;
            this.$refs.sceneList.selectSceneId = this.form.recoverId;
            this.$refs.sceneList.getList();
        },
        /**获取选中的产品或设备
         * type 1=设备集合，2=产品
         */
        getSelectProductDevice(data, type) {
            console.log(data, type, '9999999');
            if (this.currentType == null) {
                return;
            }
            if (this.currentType == 'trigger') {
                if (type == 1) {
                    this.formJson.triggers[this.currentIndex].deviceNums = data.deviceNums;
                    this.formJson.triggers[this.currentIndex].deviceCount = data.deviceNums.length;
                    this.formJson.triggers[this.currentIndex].productId = data.productId;
                    this.formJson.triggers[this.currentIndex].productName = data.productName;
                } else if (type == 2) {
                    this.formJson.triggers[this.currentIndex].deviceNums = [];
                    this.formJson.triggers[this.currentIndex].deviceCount = 0;
                    this.formJson.triggers[this.currentIndex].productId = data.productId;
                    this.formJson.triggers[this.currentIndex].productName = data.productName;
                }

                // 获取物模型
                cacheJsonThingsModel(data.productId).then((response) => {
                    let data = JSON.parse(response.data);
                    this.formJson.triggers[this.currentIndex].thingsModel = this.formatArrayIndex(data);
                    // 清楚之前数据
                    this.formJson.triggers[this.currentIndex].type = 1;
                    this.formJson.triggers[this.currentIndex].parentId = '';
                    this.formJson.triggers[this.currentIndex].parentName = '';
                    this.formJson.triggers[this.currentIndex].parentModel = null;
                    this.formJson.triggers[this.currentIndex].model = null;
                    this.formJson.triggers[this.currentIndex].operator = '';
                    this.formJson.triggers[this.currentIndex].id = '';
                    this.formJson.triggers[this.currentIndex].name = '';
                    this.formJson.triggers[this.currentIndex].value = '';
                    this.formJson.triggers[this.currentIndex].arrayIndex = '';
                    this.formJson.triggers[this.currentIndex].arrayIndexName = '';
                    // 解决数组在界面中不更新问题
                    this.$set(this.formJson.triggers, this.currentIndex, this.formJson.triggers[this.currentIndex]);
                });
            } else if (this.currentType == 'action') {
                if (type == 1) {
                    this.formJson.actions[this.currentIndex].deviceNums = data.deviceNums;
                    this.formJson.actions[this.currentIndex].deviceCount = data.deviceNums.length;
                    this.formJson.actions[this.currentIndex].productId = data.productId;
                    this.formJson.actions[this.currentIndex].productName = data.productName;
                } else if (type == 2) {
                    this.formJson.actions[this.currentIndex].deviceNums = [];
                    this.formJson.actions[this.currentIndex].deviceCount = 0;
                    this.formJson.actions[this.currentIndex].productId = data.productId;
                    this.formJson.actions[this.currentIndex].productName = data.productName;
                }

                // 获取物模型
                cacheJsonThingsModel(data.productId).then((response) => {
                    let data = JSON.parse(response.data);
                    this.formJson.actions[this.currentIndex].thingsModel = this.formatArrayIndex(data);
                    // 清楚之前数据
                    this.formJson.actions[this.currentIndex].type = 1;
                    this.formJson.actions[this.currentIndex].parentId = '';
                    this.formJson.actions[this.currentIndex].parentModel = null;
                    this.formJson.actions[this.currentIndex].parentName = '';
                    this.formJson.actions[this.currentIndex].model = null;
                    this.formJson.actions[this.currentIndex].operator = '';
                    this.formJson.actions[this.currentIndex].id = '';
                    this.formJson.actions[this.currentIndex].name = '';
                    this.formJson.actions[this.currentIndex].value = '';
                    this.formJson.actions[this.currentIndex].arrayIndex = '';
                    this.formJson.actions[this.currentIndex].arrayIndexName = '';
                    // 过滤监测数据和只读数据
                    if (this.formJson.actions[this.currentIndex].thingsModel.properties) {
                        this.formJson.actions[this.currentIndex].thingsModel.properties = this.formJson.actions[this.currentIndex].thingsModel.properties.filter((item) => item.isMonitor == 0 && item.isReadonly == 0);
                        for (let i = 0; i < this.formJson.actions[this.currentIndex].thingsModel.properties.length; i++) {
                            if (this.formJson.actions[this.currentIndex].thingsModel.properties[i].datatype.params) {
                                this.formJson.actions[this.currentIndex].thingsModel.properties[i].datatype.params = this.formJson.actions[this.currentIndex].thingsModel.properties[i].datatype.params.filter(
                                    (item) => item.isMonitor == 0 && item.isReadonly == 0
                                );
                            }
                        }
                    }
                    if (this.formJson.actions[this.currentIndex].thingsModel.functions) {
                        this.formJson.actions[this.currentIndex].thingsModel.functions = this.formJson.actions[this.currentIndex].thingsModel.functions.filter((item) => item.isReadonly == 0);
                        for (let i = 0; i < this.formJson.actions[this.currentIndex].thingsModel.functions.length; i++) {
                            if (this.formJson.actions[this.currentIndex].thingsModel.functions[i].datatype.params) {
                                this.formJson.actions[this.currentIndex].thingsModel.functions[i].datatype.params = this.formJson.actions[this.currentIndex].thingsModel.functions[i].datatype.params.filter(
                                    (item) => item.isMonitor == 0 && item.isReadonly == 0
                                );
                            }
                        }
                    }
                    // 解决数组在界面中不更新问题
                    this.$set(this.formJson.actions, this.currentIndex, this.formJson.actions[this.currentIndex]);
                });
            }
        },
        getSceneData(data) {
            this.form.recoverId = data.sceneId;
            this.form.recoverSceneName = data.sceneName;
        },
        handleSelectDevice1(type, item, index, index1) {
            this.currentType1 = type;
            this.currentIndex1 = index;
            this.currentIndexItem = index1;
            this.$refs.deviceList1.queryParams.pageNum = 1;
            this.$refs.deviceList1.openDeviceList = true;
            this.$refs.deviceList1.selectDeviceNums = item.deviceNums;
            this.$refs.deviceList1.productId = item.productId;
            this.$refs.deviceList1.productName = item.productName;
            this.$refs.deviceList1.queryParams.productId = item.productId;
            console.log(this.$refs.deviceList.queryParams, '1111111');
            this.$refs.deviceList1.getList();
        },
        handleActionParentModelChange1(identifier, index, val, itemIndex, item) {
            const nowData = val.thingsModel.properties.find((item) => {
                return item.id == identifier;
            });
            this.formJson.triggers[itemIndex].sceneOperation[index].name = nowData.name;
            item.showOperator = true;

            this.$set(this.formJson.triggers, itemIndex, this.formJson.triggers[itemIndex]);
        },
        jumpUnit(index) {
            const datas = this.formJson.triggers[index].sceneOperation && this.formJson.triggers[index].sceneOperation[0].thingsModel;
            let units = '';
            if (datas) {
                datas.properties &&
                    datas.properties.map((item) => {
                        if (item.id == this.formJson.triggers[index].sceneOperation[0].id) {
                            units = item.datatype.unit;
                        }
                    });
            }
            return units;
        },
        operatorChange(itemIndex) {
            this.$set(this.formJson.triggers, itemIndex, this.formJson.triggers[itemIndex]);
        },
        getSelectProductDevice1(data, type) {
            if (this.currentType1 == null) {
                return;
            }
            const currentIndexItem = this.currentIndexItem;
            this.formJson.triggers[currentIndexItem].deviceNums = data.deviceNums;
            this.formJson.triggers[currentIndexItem].deviceCount = data.deviceNums.length;
            this.formJson.triggers[currentIndexItem].productId = data.productId;
            this.formJson.triggers[currentIndexItem].productName = data.productName;
            console.log(this.formJson.triggers, this.currentIndex1);
            this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].deviceNums = data.deviceNums;
            this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].serialNumber = data.deviceNums[0];
            this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].deviceCount = data.deviceNums.length;
            this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].productId = data.productId;
            this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].productName = data.productName;
            // 获取物模型
            cacheJsonThingsModel(data.productId).then((response) => {
                let data = JSON.parse(response.data);
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].thingsModel = this.formatArrayIndex(data);
                // 清楚之前数据
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].type = 1;
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].parentId = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].parentName = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].parentModel = null;
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].model = null;
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].operator = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].id = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].name = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].value = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].arrayIndex = '';
                this.formJson.triggers[currentIndexItem].sceneOperation[this.currentIndex1].arrayIndexName = '';
                // 解决数组在界面中不更新问题
                this.$set(this.formJson.triggers, this.currentIndexItem, this.formJson.triggers[this.currentIndexItem]);
                console.log(this.formJson.triggers, '11111111111111');
            });
        },
        handleSceneRule(val, index, item) {
            const choosed = this.sceneRuleList.find((item) => {
                return item.value == val;
            });
            if (!val) {
                item.sceneRule[0].ruleValue = '';
            }
            item.sceneRule[0].unit = choosed.unit;
        },
        handleDelOne(index, inx) {
            this.formJson.triggers[index].sceneOperation.splice(inx, 1);
            this.$set(this.formJson.triggers, index, this.formJson.triggers[index]);
        },
        handleAddOne(index) {
            this.formJson.triggers[index].sceneOperation.push({
                deviceCount: 0,
            });
            this.$set(this.formJson.triggers, index, this.formJson.triggers[index]);
        },
        // 物模型格式化处理
        formatArrayIndex(data) {
            let obj = { ...data };
            for (let o in obj) {
                obj[o] = obj[o].map((item) => {
                    if (item.datatype.type === 'array') {
                        let arrayModel = [];
                        for (let k = 0; k < item.datatype.arrayCount; k++) {
                            let index = k > 9 ? String(k) : '0' + k;
                            if (item.datatype.arrayType === 'object') {
                                arrayModel.push({
                                    id: index,
                                    name: item.name + ' ' + (k + 1),
                                });
                            } else {
                                arrayModel.push({
                                    id: index,
                                    name: item.name + ' ' + (k + 1),
                                });
                            }
                        }
                        item.datatype.arrayModel = arrayModel;
                    }
                    return item;
                });
            }
            return obj;
        },
        // 触发器类型改变事件
        handleTriggerTypeChange(source, index) {
            this.formJson.triggers[index].id = '';
            this.formJson.triggers[index].name = '';
            this.formJson.triggers[index].model = null;
            this.formJson.triggers[index].operator = '';
            this.formJson.triggers[index].value = '';
            this.formJson.triggers[index].valueA = '';
            this.formJson.triggers[index].valueB = '';
            this.formJson.triggers[index].parentId = '';
            this.formJson.triggers[index].parentName = '';
            this.formJson.triggers[index].parentModel = null;
            this.formJson.triggers[index].arrayIndex = '';
            this.formJson.triggers[index].arrayIndexName = '';
        },
        // 触发器父级物模型选择
        handleTriggerParentModelChange(identifier, index) {
            this.formJson.triggers[index].operator = '';
            this.formJson.triggers[index].value = '';
            this.formJson.triggers[index].valueA = '';
            this.formJson.triggers[index].valueB = '';
            this.formJson.triggers[index].arrayIndex = '';
            this.formJson.triggers[index].arrayIndexName = '';
            this.formJson.triggers[index].model = null;

            let sceneScripts = [];
            if (this.formJson.triggers[index].type == 1) {
                // 属性
                sceneScripts = this.formJson.triggers[index].thingsModel.properties;
            } else if (this.formJson.triggers[index].type == 2) {
                //功能
                sceneScripts = this.formJson.triggers[index].thingsModel.functions;
            } else if (this.formJson.triggers[index].type == 3) {
                //事件
                sceneScripts = this.formJson.triggers[index].thingsModel.events;
            }
            // 物模型赋值
            for (let i = 0; i < sceneScripts.length; i++) {
                if (sceneScripts[i].id == identifier) {
                    this.formJson.triggers[index].parentName = sceneScripts[i].name;
                    this.formJson.triggers[index].parentModel = sceneScripts[i];
                    if (sceneScripts[i].datatype.type === 'object') {
                        // 对象类型
                        this.formJson.triggers[index].id = '';
                        this.formJson.triggers[index].name = '';
                    } else if (sceneScripts[i].datatype.type === 'array' && sceneScripts[i].datatype.arrayType === 'object') {
                        // 对象数组类型
                        this.formJson.triggers[index].id = '';
                        this.formJson.triggers[index].name = '';
                    } else if (sceneScripts[i].datatype.type === 'array' && sceneScripts[i].datatype.arrayType !== 'object') {
                        // 普通类型，数组类
                        this.formJson.triggers[index].id = sceneScripts[i].id;
                        this.formJson.triggers[index].name = sceneScripts[i].name;
                        // 用于减少界面判断
                        this.formJson.triggers[index].model = {
                            datatype: {
                                type: this.formJson.triggers[index].parentModel.datatype.arrayType,
                                maxLength: -1,
                                min: -1,
                                max: -1,
                                unit: this.$t('scene.index.670805-64'),
                            },
                        };
                    } else {
                        // 普通类型,不包含数组类型
                        this.formJson.triggers[index].id = sceneScripts[i].id;
                        this.formJson.triggers[index].name = sceneScripts[i].name;
                        this.formJson.triggers[index].model = sceneScripts[i];
                    }
                    break;
                }
            }
            //能源触发的点击逻辑
            if (this.formJson.triggers[index].source === 6) {
                console.log('11111111');
                const data = {
                    identifier: identifier,
                    deviceNum: this.formJson.triggers[index].deviceNums[index],
                };
                getEnergyType(data).then((rp) => {
                    if (rp.Type != null) {
                        console.log(rp.Type);
                        this.formJson.triggers[index].energtype = rp.Type;
                    }
                    this.tpyeopen = true;
                });
            }
        },
        // 触发器-物模数组索引选择
        handleTriggerIndexChange(id, index) {
            this.formJson.triggers[index].arrayIndexName = this.formJson.triggers[index].parentModel.datatype.arrayModel.find((x) => x.id == id).name;
            this.formJson.triggers[index].value = '';
            this.formJson.triggers[index].valueA = '';
            this.formJson.triggers[index].valueB = '';
            this.formJson.triggers[index].operator = '';
            // 数组类型保留id和name
            if (this.formJson.triggers[index].parentModel.datatype.arrayType === 'object') {
                this.formJson.triggers[index].id = '';
                this.formJson.triggers[index].name = '';
            }
        },
        // 触发器物模选择
        handleTriggerModelChange(id, index) {
            this.formJson.triggers[index].operator = '';
            this.formJson.triggers[index].value = '';
            this.formJson.triggers[index].valueA = '';
            this.formJson.triggers[index].valueB = '';
            let model = null;
            if (this.formJson.triggers[index].parentModel.datatype.type === 'array' || this.formJson.triggers[index].parentModel.datatype.type === 'object') {
                model = this.formJson.triggers[index].parentModel.datatype.params.find((item) => item.id == id);
                this.formJson.triggers[index].name = model.name;
                this.formJson.triggers[index].model = model;
            }
        },
        // 添加触发器
        handleAddTrigger() {
            this.formJson.triggers.push({
                source: 1, //1=设备，2=定时,3=产品
                type: 1, // 类型
                id: '',
                name: '',
                operator: '',
                value: '',
                // between操作符时，值=值A-值B
                valueA: '',
                valueB: '',
                model: null,
                parentModel: null,
                parentId: '', // 物模父级id
                parentName: '',
                arrayIndex: '', // 索引，数组才有
                arrayIndexName: '',
                isAdvance: 0, // 自定义CRON
                cronExpression: '', // cron表达式
                timerTimeValue: '', // 时间
                timerWeekValue: [1, 2, 3, 4, 5, 6, 7], // 星期
                productId: 0,
                productName: '',
                deviceNums: [],
                deviceCount: 0,
                scriptPurpose: 2, // scriptPurpose:脚本用途(1=数据流，2=触发器，3=执行动作)
                sceneRule: [
                    {
                        ruleType: 0,
                        unit: '',
                        ruleValue: '',
                    },
                ],
            });
        },
        // 添加动作
        handleAddAction() {
            if (this.formJson.actions[this.formJson.actions.length - 1].source == 6) {
                if (this.formJson.actions[this.formJson.actions.length - 1].userName.length <= 0) {
                    return Message.error('请输入执行人！');
                }
            }
            this.formJson.actions.push({
                source: 1, //1=设备，3=产品，4=告警
                type: 2, // 类型
                id: '',
                name: '',
                value: '',
                model: null,
                parentId: '', // 物模id
                parentName: '',
                parentModel: null,
                arrayIndex: '', // 索引，数组才有
                arrayIndexName: '',
                productId: 0,
                productName: '',
                deviceNums: [],
                deviceCount: 0,
                scriptPurpose: 3, // scriptPurpose:脚本用途(1=数据流，2=触发器，3=执行动作)
                userName: '',
            });
        },
        // 删除触发器
        handleRemoveTrigger(index) {
            this.formJson.triggers.splice(index, 1);
        },
        // 删除动作
        handleRemoveAction(index) {
            this.formJson.actions.splice(index, 1);
        },
        /** cron表达式按钮操作 */
        handleShowCron(item, index) {
            this.expression = item.cronExpression;
            this.triggerIndex = index;
            this.openCron = true;
        },
        /** 确定后回传值 */
        crontabFill(value) {
            this.formJson.triggers[this.triggerIndex].cronExpression = value;
        },
        /** 星期改变事件 **/
        weekChange(data, index) {
            this.gentCronExpression(index);
        },
        /** 时间改变事件 **/
        timeChange(data, index) {
            this.gentCronExpression(index);
        },
        /**自定义cron表达式选项改变事件 */
        customerCronChange(data, index) { },
        /** 生成cron表达式**/
        gentCronExpression(index) {
            let hour = '00';
            let minute = '00';
            if (this.formJson.triggers[index].timerTimeValue != null && this.formJson.triggers[index].timerTimeValue != '') {
                hour = this.formJson.triggers[index].timerTimeValue.substring(0, 2);
                minute = this.formJson.triggers[index].timerTimeValue.substring(3);
            }
            let week = '*';
            if (this.formJson.triggers[index].timerWeekValue.length > 0) {
                let order = this.formJson.triggers[index].timerWeekValue.slice().sort();
                for (let i = 0; i < order.length; i++) {
                    if (order[i] != 7) {
                        order[i] = order[i] + 1;
                    } else {
                        order[i] = 1;
                    }
                }
                console.log(order);
                week = order;
                //week = this.formJson.triggers[index].timerWeekValue.sort();
                console.log(week);
            }
            this.formJson.triggers[index].cronExpression = '0 ' + minute + ' ' + hour + ' ? * ' + week;
        },
        // 动作类型改变事件
        handleActionTypeChange(data, index) {
            this.formJson.actions[index].id = '';
            this.formJson.actions[index].name = '';
            this.formJson.actions[index].value = '';
            this.formJson.actions[index].model = null;
            this.formJson.actions[index].parentId = '';
            this.formJson.actions[index].parentName = '';
            this.formJson.actions[index].arrayIndex = '';
            this.formJson.actions[index].arrayIndexName = '';
            this.formJson.actions[index].parentModel = null;
            this.formJson.actions[index].userName = null;
            this.formJson.actions[index].userId = null;
        },
        // 动作物模型项改变事件
        handleActionParentModelChange(identifier, index) {
            this.formJson.actions[index].model = null;
            this.formJson.actions[index].value = '';
            this.formJson.actions[index].arrayIndex = '';
            this.formJson.actions[index].arrayIndexName = '';

            let sceneScripts = [];
            if (this.formJson.actions[index].type == 1) {
                //属性
                sceneScripts = this.formJson.actions[index].thingsModel.properties;
            } else if (this.formJson.actions[index].type == 2) {
                //功能
                sceneScripts = this.formJson.actions[index].thingsModel.functions;
            }
            for (let i = 0; i < sceneScripts.length; i++) {
                if (sceneScripts[i].id == identifier) {
                    this.formJson.actions[index].parentName = sceneScripts[i].name;
                    this.formJson.actions[index].parentModel = sceneScripts[i];
                    if (sceneScripts[i].datatype.type === 'object') {
                        // 对象类型
                        this.formJson.actions[index].id = '';
                        this.formJson.actions[index].name = '';
                    } else if (sceneScripts[i].datatype.type === 'array' && sceneScripts[i].datatype.arrayType === 'object') {
                        // 对象数组类型
                        this.formJson.actions[index].id = '';
                        this.formJson.actions[index].name = '';
                    } else if (sceneScripts[i].datatype.type === 'array' && sceneScripts[i].datatype.arrayType !== 'object') {
                        // 普通类型，数组类
                        this.formJson.actions[index].id = sceneScripts[i].id;
                        this.formJson.actions[index].name = sceneScripts[i].name;
                        // 用于减少界面判断
                        this.formJson.actions[index].model = {
                            datatype: {
                                type: this.formJson.actions[index].parentModel.datatype.arrayType,
                                maxLength: -1,
                                min: -1,
                                max: -1,
                                unit: this.$t('scene.index.670805-64'),
                            },
                        };
                    } else {
                        // 普通类型,不包含数组类型
                        this.formJson.actions[index].id = sceneScripts[i].id;
                        this.formJson.actions[index].name = sceneScripts[i].name;
                        this.formJson.actions[index].model = sceneScripts[i];
                    }
                    break;
                }
            }
        },
        // 执行动作物模型数组索引选择
        handleActionIndexChange(id, index) {
            this.formJson.actions[index].arrayIndexName = this.formJson.actions[index].parentModel.datatype.arrayModel.find((x) => x.id == id).name;
            this.formJson.actions[index].value = '';
            this.formJson.actions[index].valueA = '';
            this.formJson.actions[index].valueB = '';
            this.formJson.actions[index].operator = '';
            // 数组类型保留id和name
            if (this.formJson.actions[index].parentModel.datatype.arrayType === 'object') {
                this.formJson.actions[index].id = '';
                this.formJson.actions[index].name = '';
            }
        },
        // 执行动作物模型选择
        handleActionModelChange(id, index) {
            this.formJson.actions[index].operator = '';
            this.formJson.actions[index].value = '';
            let model = null;
            if (this.formJson.actions[index].parentModel.datatype.type === 'array' || this.formJson.actions[index].parentModel.datatype.type === 'object') {
                model = this.formJson.actions[index].parentModel.datatype.params.find((item) => item.id == id);
                this.formJson.actions[index].name = model.name;
                this.formJson.actions[index].model = model;
            }
        },
        // 提交按钮
        handleSubmitForm() {
            if (this.formJson.actions[this.formJson.actions.length - 1].source == 6) {
                if (this.formJson.actions[this.formJson.actions.length - 1].userName.length <= 0) {
                    return Message.error('请输入执行人！');
                }
            }
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    let triggers = [];
                    let actions = [];
                    // 触发器验证和构建
                    for (let i = 0; i < this.formJson.triggers.length; i++) {
                        // 类型为属性/功能/事件
                        if (this.formJson.triggers[i].type == 1 || this.formJson.triggers[i].type == 2 || this.formJson.triggers[i].type == 3) {
                            // 值不能为空
                            if (this.formJson.triggers[i].source == 1) {
                                if (this.formJson.triggers[i].value == '') {
                                    this.$modal.alertError(this.$t('scene.index.670805-67'));
                                    return;
                                } else if (this.formJson.triggers[i].value.indexOf('-') != -1) {
                                    // 区间格式10-50
                                    if (this.formJson.triggers[i].valueA == '' || this.formJson.triggers[i].valueB == '') {
                                        this.$modal.alertError(this.$t('scene.index.670805-68'));
                                        return;
                                    }
                                }
                            }
                            // 定时时间不能为空
                            if (this.formJson.triggers[i].source == 2) {
                                if (this.formJson.triggers[i].isAdvance == 0) {
                                    if (this.formJson.triggers[i].timerTimeValue == '' || this.formJson.triggers[i].timerTimeValue == null) {
                                        this.$modal.alertError(this.$t('scene.index.670805-69'));
                                        return;
                                    }
                                    if (this.formJson.triggers[i].timerWeekValue == null || this.formJson.triggers[i].timerWeekValue == '') {
                                        this.$modal.alertError(this.$t('scene.index.670805-70'));
                                        return;
                                    }
                                } else if (this.formJson.triggers[i].isAdvance == 1) {
                                    if (this.formJson.triggers[i].cronExpression == '') {
                                        this.$modal.alertError(this.$t('scene.index.670805-71'));
                                        return;
                                    }
                                }
                            }
                        }
                        // 数据重组
                        let item = this.formJson.triggers[i];
                        // id拼接array索引
                        if (item.arrayIndex != '') {
                            item.id = 'array_' + item.arrayIndex + '_' + item.id;
                        }
                        // 只传需要的数据
                        triggers[i] = {
                            productId: item.productId,
                            productName: item.productName,
                            deviceNums: item.deviceNums,
                            deviceCount: item.deviceCount,
                            source: item.source,
                            type: item.type,
                            id: item.id,
                            name: item.name,
                            operator: item.operator,
                            value: item.value,
                            isAdvance: item.isAdvance,
                            cronExpression: item.cronExpression,
                            parentId: item.parentId,
                            parentName: item.parentName,
                            arrayIndex: item.arrayIndex,
                            arrayIndexName: item.arrayIndexName,
                            scriptPurpose: 2, // scriptPurpose:脚本用途(1=数据流，2=触发器，3=执行动作)
                            sceneOperation: item.sceneOperation,
                            sceneRule: item.sceneRule,
                            energtype: item.energtype,
                        };
                        if (item.source === 5) {
                            triggers[i].name = item.sceneOperation[item.sceneOperation.length - 1].name;
                            triggers[i].parentName = item.sceneOperation[item.sceneOperation.length - 1].name;
                            triggers[i].id = item.sceneOperation[item.sceneOperation.length - 1].id;
                            triggers[i].parentId = item.sceneOperation[item.sceneOperation.length - 1].id;
                        }
                    }
                    // 动作验证和构建
                    for (let i = 0; i < this.formJson.actions.length; i++) {
                        if (this.formJson.actions[i].value === '' && this.formJson.actions[i].source !== 4 && this.formJson.actions[i].source !== 5 && this.formJson.actions[i].source !== 6) {
                            this.$modal.alertError(this.$t('scene.index.670805-72'));
                            return;
                        }
                        // 数据重组
                        let item = this.formJson.actions[i];
                        // id拼接array索引
                        if (item.arrayIndex != '') {
                            item.id = 'array_' + item.arrayIndex + '_' + item.id;
                        }
                        // 只传需要的数据
                        actions[i] = {
                            productId: item.productId,
                            productName: item.productName,
                            deviceCount: item.deviceCount,
                            source: item.source,
                            deviceNums: item.deviceNums,
                            type: item.type,
                            id: item.id,
                            name: item.name,
                            value: item.value,
                            parentId: item.parentId,
                            parentName: item.parentName,
                            arrayIndex: item.arrayIndex,
                            arrayIndexName: item.arrayIndexName,
                            scriptPurpose: 3, // scriptPurpose:脚本用途(1=数据流，2=触发器，3=执行动作)
                            userId: item.userId,
                        };
                    }
                    // 判断是否包含告警(1=包含，2=不包含)
                    if (actions.filter((x) => x.source === 4).length > 0) {
                        this.form.hasAlert = 1;
                    } else {
                        this.form.hasAlert = 2;
                    }
                    this.form.triggers = triggers;
                    this.form.actions = actions;
                    this.confirmLoading = true;
                    this.form.triggers.forEach((item) => {
                        item.sceneEneregy = {
                            productId: item.productId,
                            productName: item.productName,
                            id: item.id,
                            name: item.name,
                            serialNumber: item.deviceNums[0],
                            energtype: item.energtype,
                        };
                    });
                    if (this.form.sceneId != null) {
                        updateScene(this.form).then(() => {
                            this.$modal.msgSuccess(this.$t('scene.index.670805-73'));
                            this.open = false;
                            this.confirmLoading = false;
                            this.getList();
                        });
                    } else {
                        addScene(this.form).then(() => {
                            this.$modal.msgSuccess(this.$t('scene.index.670805-74'));
                            this.open = false;
                            this.confirmLoading = false;
                            this.getList();
                        });
                    }
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.scene-wrap {
    padding: 6px;

    .search-wrap {
        margin-bottom: 6px;
    }

    .content-wrap {
        padding-bottom: 100px;
    }
}

.scene-config-dialog {

    .condition-wrap,
    .action-wrap {
        position: relative;

        .trigger-type-wrap {
            margin-bottom: 10px;

            ::v-deep .el-input__inner {
                /**border: none;*/
                box-shadow: none;
            }
        }

        .item-wrap {
            margin-bottom: 15px;
            padding: 10px;
            background-color: #d9e5f6;
            border-radius: 5px;

            .delete-wrap {
                position: absolute;
                right: 10px;
                top: 0;
            }
        }
    }
}
</style>
